<template>
    <div class="app-container">
        <!-- <v-GetToolBar align="right" :MenuCode="MenuCode" :IsfunEdit='IsfunEdit' :Isfunfail='Isfunfail' @handeAdd='handeAdd'
          @handleEdit='handleEdit' @OpenFindForm='OpenFindForm' @handlexportData='handlexportData'
          @IsfunEdit='handleIsfunEdit' @IsfunDelete='handleIsfunfail' @handleclose='handleclose'></v-GetToolBar> -->

        <div class="ces-main">

            <div class="main" style="width: 23vw;margin-right: 4px;">
                <el-button type="primary" size="small" class="botn" @click="addProduct"> <i
                        class="el-icon-circle-plus-outline"></i> 添加</el-button>
                <el-button @click="searchWo" type="success" size="small" class="botn float"><i class="el-icon-search"></i>
                    查询</el-button>
                <el-input size="small" class="inp" v-model="SearchWO.PrdName" placeholder="物料名称"></el-input>
                <el-input size="small" class="inp" v-model="SearchWO.PrdNo" placeholder="产品编号"></el-input>
                <!-- <div class="vxe"> -->
                <ComVxeTable class="vxe" size='small' :isSelection='false' :isIndex='true' :isSearch='true'
                    :isHandle='false' :ToolBarheight="167" :IsfunEdit='true' :Isfunfail='true' :tableData='tableData'
                    :tableCols='tableCols' :Search='SearchWO' @refresh="refresh" @handleEdit='handleEdit' @select="handselect"
                    @selectAll="handselectAll" @handleDelete="delDataWo" @handrowclick="handrowclick">
                </ComVxeTable>
                <!-- </div> -->
            </div>

            <div class="main" style="width: 63vw;">
                <el-button type="primary" size="small" class="botn" @click="addDefect"> <i
                        class="el-icon-circle-plus-outline"></i> 添加</el-button>
                <el-button @click="searchWoT" type="success" size="small" class="botn float"><i class="el-icon-search"></i>
                    查询</el-button>
                <el-input size="small" class="inp" v-model="SearchSSN.PrdName" placeholder="物料名称"></el-input>
                <!-- <el-input size="small" class="inp" v-model="PrdNo" placeholder="产品编号"></el-input> -->
                <ComVxeTable class="vxe" size='mini' :isSelection='false' :isIndex='true' :isSearch='true' :isHandle='false'
                    :ToolBarheight="167" :IsfunEdit='true' :Isfunfail='true' :tableData='tableDataCode'
                    :tableCols='tableColsCode' :Search='SearchSSN' @refresh="refresh" @handleEdit='handleEditDefect'
                    @select="handselectCode" @selectAll="handselectAllCode" @handleDelete="delData"
                    @handrowclick="handrowclickCode">
                </ComVxeTable>
            </div>

        </div>

        <el-dialog :visible.sync="dialogVisible" width="800px" top="20px">
            <!-- {{this.FormData}} -->
            <el-form :model="name" label-width="80px" label-position="right">
                <el-tabs type="border-card">
                    <el-tab-pane label="">
                        <el-form :model="FormData" label-width="90px" ref="MianForm">
                            <Mian-Form ref="form" labelWidth='100px' :FormData='FormData' :tableCols='tableCols' :StepSN='0'
                                :searchHandle="searchHandle"></Mian-Form>
                        </el-form>
                    </el-tab-pane>
                    <div style="float:right ">
                        <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
                        <el-button type="primary" @click="handleSave()">保存</el-button>
                    </div>
                </el-tabs>
            </el-form>
        </el-dialog>

        <el-dialog :visible.sync="dialogVisibleDefect" width="800px" top="20px">
            <!-- {{this.FormData}} -->
            <el-form :model="name" label-width="80px" label-position="right">
                <el-tabs type="border-card">
                    <el-tab-pane label="">
                        <el-form :model="FormData" label-width="90px" ref="MianForm">
                            <Mian-Form ref="form" labelWidth='100px' :FormData='FormDataDefect' :tableCols='tableColsCode'
                                :StepSN='0' :searchHandle="searchHandle"></Mian-Form>
                        </el-form>
                    </el-tab-pane>
                    <div style="float:right ">
                        <el-button type="primary" @click="dialogVisibleDefect = false">关闭</el-button>
                        <el-button type="primary" @click="handleSaveDefect">保存</el-button>
                    </div>
                </el-tabs>
            </el-form>
        </el-dialog>


    </div>
</template>
  
<script>


//引入组件

import ComVxeTable from "../Common//ComVxeTable.vue";
import SearchForm from "../Common//Search.vue";
import MianForm from "../Common//form.vue";
//, getDataList,getTableCols  #78fff7
import { funDownload, GetTableColsData } from "@/api/Common";

import { getLodop } from "@/api/LodopFuncs";

import { getDataList, DeleteForm, UpdateForm } from '@/api/CommonPage/CommonPage'; //更改
import { MESSSN, MESSSNUpdate } from '@/api/Collection/BarcodePrinting'; //更改

import { CloseForm } from '@/api/System';
import vGetToolBar from "../Common/GetToolBar";

export default {
    //Collection/BarcodePrinting
    name: "ProductDefects",
    data() {
        //表单查询一般分为input，下拉框，和日期，下面是下拉框时的设置下拉信息的情况，我的项目没有用到，但是留着方便看

        return {
            FormDataDefect: {},
            dialogVisibleDefect: false,
            FormDataInfo: [],
            FormData: {},

            PrdNameT: '',
            PrdName: '',
            PrdNo: '',
            radio: '3',
            row: [],
            createQty: null,
            PrdNo: '',
            selectCode: [],
            SelectData: {},
            reCode: {},
            tableDataCode: [],
            tableColsCode: [],
            formInline: {
                user: '',
                region: ''
            },
            MenuCode: "A050106",
            tableData: [],//表格数据
            tableCols: [],//表格列数据
            searchHandle: [],
            billID: "",
            IsfunEdit: false,
            Isfunfail: true,
            SearchWO: {
                // 查询条件
                total: 0, // 总记录数
                pageSize: 14, // 一页显示的记录数
                page: 1, // /当前页码
                RNO: "1614", // 表编号  根据Controller中对应的表编号
                InterID: "1614",
                jsWhere: "", // 自定义条件
                Download: ""
                // Status: 0,
                //   BillState: "BillStateALL",
            },
            SearchSSN: {
                // 查询条件
                total: 0, // 总记录数
                pageSize: 14, // 一页显示的记录数
                page: 1, // /当前页码
                RNO: "1615", // 表编号  根据Controller中对应的表编号
                InterID: "1615",
                jsWhere: "", // 自定义条件
                IsFieldText: '0',
                Download: ""
                // Status: 0,
                //   BillState: "BillStateALL",
            },

            SearchDialog: false,
            DialogBtnMargin: 0,
            dialogVisible: false,
            dialogFormVisible: false,
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: ''
            },


        }
    },

    components: { //引入组件后注册组件
        vGetToolBar,
        SearchForm,
        MianForm,
        ComVxeTable
    },
    mounted() {
        this.getDataList()
        // this.SearchCode.total = 12;
    },
    methods: {
        async searchWo() {
            const res = await getDataList({ data: JSON.stringify(this.SearchWO) });
            this.tableData = res.data.Table;
            this.SearchWO.total = res.data.Table1[0].RecordCount
            // debugger
        },
        async searchWoT() {
            const res = await getDataList({ data: JSON.stringify(this.SearchSSN) });
            this.tableDataCode = res.data.Table;
            this.SearchSSN.total = res.data.Table1[0].RecordCount
            // debugger
        },
        async handleSaveDefect() {
            const result = await UpdateForm({
                IsPostBack: true,
                InterID: this.SearchSSN.InterID,
                SavaDate: JSON.stringify(this.FormDataDefect)
            });
            if (result.code === 20000) {
                this.$notify({
                    title: result.msg,
                    dangerouslyUseHTMLString: true,
                    message: "",
                    type: "success",
                });
                // this.getDataList();
            } else {
                this.$notify({
                    title: result.msg,
                    dangerouslyUseHTMLString: true,
                    message: "",
                    type: "error",
                });
            }
            this.handrowclick();
            this.dialogVisibleDefect = false
        },

        async handleSave() {
            const result = await UpdateForm({
                IsPostBack: true,
                InterID: this.SearchWO.InterID,
                SavaDate: JSON.stringify(this.FormData)
            });
            if (result.code === 20000) {
                this.$notify({
                    title: result.msg,
                    dangerouslyUseHTMLString: true,
                    message: "",
                    type: "success",
                });
                // this.getDataList();
            } else {
                this.$notify({
                    title: result.msg,
                    dangerouslyUseHTMLString: true,
                    message: "",
                    type: "error",
                });
            }
            const res = await getDataList({ data: JSON.stringify(this.SearchWO) });
            this.tableData = res.data.Table;
            this.Search.total = res.data.Table.length;
            this.dialogVisible = false
        },

        async addProduct() {
            const result = await UpdateForm({
                IsPostBack: false,
                InterID: this.SearchWO.InterID,
            });
            this.FormData = result.data1.Table[0];
            this.dialogVisible = true;
        },

        async addDefect() {
            const result = await UpdateForm({
                IsPostBack: false,
                InterID: this.SearchSSN.InterID,
            });
            this.FormDataDefect = result.data1.Table[0];
            this.FormDataDefect.PrdNo = this.PrdNo
            // debugger
            this.dialogVisibleDefect = true;
        },



        onSubmit() {
        },
        //搜索重置
        refresh() {
            this.getDataListCe();
        },
        reform() {
            this.Search = {
                // 查询条件
                // total: 0, // 总记录数
                pageSize: 14, // 一页显示的记录数
                page: 1, // /当前页码
                RNO: "4", // 表编号  根据Controller中对应的表编号
                jsWhere: "", // 自定义条件

            };
            this.getDataListCe();
        },
        async getDataList() {
            const res = await getDataList({ data: JSON.stringify(this.SearchWO) });
            this.tableData = res.data.Table;
            this.tableCols = res.data1;
            this.SearchWO.total = res.data.Table1[0].RecordCount;
            // this.Search.total = res.data.Table.length;
            // debugger

            // debugger SearchSSN
            //将data 中的数据放到DataList中
            this.SearchSSN.jsWhere = `and  1 = 2`
            const resCols = await getDataList({ data: JSON.stringify(this.SearchSSN) });
            this.tableColsCode = resCols.data1;
            // this.Search.total = res.data.Table1[0].RecordCount;
            // this.billID = "";
            // console.log(this.tableData);
        },
        OpenFindForm() {
            //打开搜索框
            this.SearchDialog = true;
            //计算外边距
            console.log(this.$refs.searchDialog.offsetHeight);
            this.DialogBtnMargin = this.$refs.searchDialog.offsetHeight - 40;
        }, handlexportData() {
            funDownload("334", JSON.stringify(this.Search));
        }, handleIsfunEdit() {
            this.IsfunEdit = true;
        }, handleIsfunfail() {
            this.Isfunfail = true;
        }, async handeAdd() {
            // 新建

            let routeUrl = this.$router.push({
                path: "/System/PageAutoForm",
                query: { billID: "", IsPostBack: false },
            });
            window.open(routeUrl.path, "_top");

        },
        async handleEdit(scope) {
            this.dialogVisible = true
            const result = await UpdateForm({
                IsPostBack: false,
                InterID: this.SearchWO.InterID,
                SavaDate: JSON.stringify(scope.row)
            });
            this.FormData = result.data1.Table[0];

        },
        async handleEditDefect(scope) {
            this.dialogVisibleDefect = true
            const result = await UpdateForm({
                IsPostBack: false,
                InterID: this.SearchSSN.InterID,
                SavaDate: JSON.stringify(scope.row)
            });
            this.FormDataDefect = result.data1.Table[0];
        },
        delData({ $index, row }) {
            this.$confirm('删除操作, 是否继续?', '提示', {
                confirmButtonText: "确认",
                cancelButtonText: "取消",
            })
                .then(async () => {


                    // debugger
                    // const result = await DeleteForm({
                    //     billID: row.SSN,
                    //     types: 1,
                    //     IsPostBack: false,
                    // });

                    const result = await DeleteForm({
                        InterID: this.SearchSSN.InterID,
                        SavaDate: JSON.stringify(row),
                        IsPostBack: false,
                    });

                    this.$message({
                        type: "success",
                        message: result.msg,
                    });
                    this.handrowclick()
                })
                .catch((err) => {
                    console.error(err);
                });
        },
        delDataWo({ $index, row }) {
            this.$confirm('删除操作, 是否继续?', '提示', {
                confirmButtonText: "确认",
                cancelButtonText: "取消",
            })
                .then(async () => {


                    // debugger
                    // const result = await DeleteForm({
                    //     billID: row.SSN,
                    //     types: 1,
                    //     IsPostBack: false,
                    // });

                    const result = await DeleteForm({
                        InterID: this.SearchWO.InterID,
                        SavaDate: JSON.stringify(row),
                        IsPostBack: false,
                    });

                    // this.getDataListCe();
                    this.$message({
                        type: "success",
                        message: result.msg,
                    });

                    const res = await getDataList({ data: JSON.stringify(this.SearchWO) });
                    this.tableData = res.data.Table;
                    // this.tableCols = res.data1;
                    // this.Search.total = res.data.Table.length;
                    this.Search.total = res.data.Table1[0].RecordCount;

                    // this.getDataList();
                })
                .catch((err) => {
                    console.error(err);
                });
        },
        async handrowclick(row) {
            // alert(this.SearchCode.total)
            if (row) {
                this.SearchSSN.jsWhere = `and PrdNo = '${row.row.PrdNo}'`
                // this.SelectData = row.row
                this.PrdNo = row.row.PrdNo
                // this.createQty = row.row.Qty - row.row.GENERATEDQTY
            }
            const reCode = await getDataList({ data: JSON.stringify(this.SearchSSN) });
            if (reCode.IsFieldText == '0') {
                this.tableColsCode = reCode.data1;
                this.SearchSSN.IsFieldText = '1'//已加载数据，不再加载
            }
            this.tableDataCode = reCode.data.Table;
            // this.tableDataCodeSave = reCode.data.Table;
            this.SearchSSN.total = reCode.data.Table1[0].RecordCount;
            // alert(this.SearchCode.total)
            // debugger
            // this.SearchCode.total = reCode.data.Table.length;
        },
        // handrowclickCode(row) {
        //     this.CodeOne = row.row
        //     // alert(this.CodeOne.SSN)
        //     // debugger
        // },
        handleclose() {


            if (this.billID != "") {

                this.$confirm('关单操作, 是否继续? 订单号：' + this.billID, '提示', {
                    confirmButtonText: "确认",
                    cancelButtonText: "取消",
                })
                    .then(async () => {



                        const result = await CloseForm({

                            billID: this.billID,
                            types: 1,
                            IsPostBack: false,
                        });
                        this.getDataListCe();
                        this.$message({
                            type: "success",
                            message: result.msg,
                        });

                    })
                    .catch((err) => {
                        console.error(err);
                    });
            } else {
                this.$notify({
                    title: "关单操作，请选择数据" + this.billID,
                    dangerouslyUseHTMLString: true,
                    message: "",
                    type: "error",
                });
            }



        },


    }
}
</script>
  
<style lang="scss" scoped>
.ces-main {
    display: flex;
    flex-wrap: nowrap;
    height: 100%;

    // width: 98vw;
    .botn {
        margin: 2px 0 2px 2px;
    }

    .main {
        flex: 1;
        background-color: #fff;
        border-radius: 4px;
        padding: 4px;

        .inp {
            float: right;
            // display: inline-block;
            width: 100px;
            margin-left: 2px;
            transform: translateY(2px);
        }

        /deep/ .el-radio__label {
            font-size: 12px
        }

        .check {
            margin-left: 12px;
            transform: translateY(2px);
            margin-right: 0;
            padding-top: 8px;
            width: 66px;

        }

        /deep/ .vxe .el-input__inner {
            height: 22px;
        }



        .float {
            float: right;
        }
    }
}

.dialog {

    /deep/ .el-dialog {
        margin-top: 50vh !important;
        transform: translateY(-50%);
    }

    table {
        width: 100%;
        border-collapse: collapse;

        /* 合并相邻的边框 */
        td {
            border: 1px solid #000;
            padding: 15px;
            /* 添加内边距 */
            text-align: left;
            /* 文字左对齐 */
        }

        tr td:first-child {
            width: 30%;
        }

    }
}



.app-container {
    background-color: rgb(255, 255, 255);

    .EditObjs-table {
        margin-top: 30px;
    }

    .permission-tree {
        margin-bottom: 5px;
    }
}

.searchDialog ::v-deep .el-dialog__header {
    background: #409eff;
}

.searchDialog ::v-deep .el-dialog__title {
    color: #fff;
}

.searchDialog ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #fff;
}
</style>